Unix Commands


Redirecting the Output  
We use the > symbol to redirect the output of a command. For example, to create a file called list1 containing a list of fruit, type  

 cat > list1
Then type in the names of some fruit. Press [Return] after each one.
pear
banana
apple
^D {this means press [Ctrl] and [d] to stop}

Appending to a file
The form >> appends standard output to a file. So to add more items to the file list1, type
cat >> list1

Then type in the names of more fruit
peach
grape
orange
^D (Control D to stop)

To read the contents of the new file, type

ls:- The ls command is a command-line utility for listing the contents of a directory or directories given to it via standard input. It writes results to standard output. The ls command supports showing a variety of information about files, sorting on a range of options and recursive listing.

For help in Unix
man – view manual pages for Unix commands

Unix Shell Commands
clear – clear screen
history – show history of previous commands
more - to display a file, screen by screen;

Time and Date commands
date – show current date and time
sleep – wait for a given number of seconds
uptime – find out how long the system has been up

Unix users commands
These commands allow you to get basic information about Unix users in your environment.
whoami – show your username
id – print user identity
groups – show which groups user belongs to
passwd – change user password
who – find out who is logged into the system
last – show history of logins into the system


Text file operations in Unix
Most of important configuration in Unix is in clear text files, these commands will let you quickly inspect files or view logs:

cat – concatenate files and show contents to the standard output
more – basic pagination when viewing text files or parsing Unix commands output
less – an improved pagination tool for viewing text files (better than more command)
head – show the first 10 lines of text file (you can specify any number of lines)
tail – show the last 10 lines of text file (any number can be specified)
grep – search for patterns in text files

Unix directory management commands
Navigating filesystems and managing directories:

cd – change directory
pwd – confirm current directory
ln – make links and symlinks to files and directories
mkdir – make new directory
rmdir – remove directories in Unix
Unix system status commands

Most useful commands for reviewing hostname configuration and vital stats:
hostname – show or set server hostname
w – display system load, who's logged in and what they are doing
uname – print Unix system information

Networking commands in Unix
Most useful commands for inspecting network setup and exploring network connections and ports:

ifconfig – show and set IP addresses (found almost everywhere)
ip – show and set IP addresses (in recent Linux versions)
ping – check if remote host is reachable via ICMP ping
netstat – show network stats and routing information

Process management
Listing processes and confirming their status, and stopping processes if needed:

ps – list processes
top – show tasks and system status
kill – kill a process (stop application running)

Remote access commands
ssh is really the only way to go, but it's important to know telnet as well:

telnet – clear-text (insecure) remote access protocol
ssh – Secure SHell – encrypted remote access client
check out the SSH reference!

File transfers  commands
Always useful to know how to copy files between servers or just download some package from the web:
ftp – clear-text (insecure!) File Transfer Protocol client
sftp – secure (encrypted) version of FTP
scp – secure (encrypted) version of cp command

wget – download files from remote servers, HTTP/HTTPS and FTP

Tail Command:- It gives last 10 lines.

tail file_name

tail -1 file_name

head command:- It gives first 10 lines.

head file_name

Print the content of the file
tail -n +2 ant5.txt | head -n -1

Check running process time using
ps -p 1388 -o etime

Print Line between M and N
head -n 20 state.txt | tail -10

Cut particular field for a file 
name,sex,house_nr,height,shoe_size
arthur,m,42,181,11.5
berta,f,101,163,8.5
chris,m,1333,175,10
don,m,77,185,12.5

elisa,f,204,166,7

cut -d',' -f2-4 abc1.txt

How to find duplicate records in file without writing sql query?
123
123
234
234
123
345

I would like to find how many times '123' was duplicated, how many times '234' was duplicated, etc. So ideally, the output would be like:

123  3
234  2
345  1

sort <file> | uniq -c

You can use the more verbose --count flag too with the GNU version, e.g., on Linux:

sort <file> | uniq --count

How to find the count of an word in the file?
grep –ow "hello" test.txt | wc –l

How to find the last 50 lines and first 50 lines in the file?
tail -50 test_text.txt
head -50 test_text.txt  > test1.txt

How to find the lines from 10 to100 in the file

tail -n +10 input.txt | head -n 91 > output.txt

How to change owner and group of file?

chgrp oracle:db input.txt

How to change permission of the file?

chmod wg+rwx input.txt

How to create , append and view the file?

cat  >  input.txt
cat >> input.txt
cat input.txt

How find the duplicate records in the file?

sort FILE | uniq -cd
sort FILE | uniq --count --repeated

How find the duplicate records counts including unique records

sort FILE | uniq -c

sort File | uniq --count

AWK commands

Awk options program file

Awk –F “,” ‘{print $0}’ test.txt

Awk –F “,” ‘{if ($1==123) print $0}’ test.txt

No comments:

Post a Comment